import numpy as np
import skimage as ski
import cv2

#信息接口到原始地图
height = 200
width = 200
#msg_list = [] 
#src_img = np.asarray(msg_list, dtype=np.uint8)
#src_img.shape = height, width

#直接得到图像的版本
src_img = cv2.imread("ex_src.png", cv2.IMREAD_GRAYSCALE)

retval, bi_img = cv2.threshold(src_img, 127, 255, cv2.THRESH_BINARY)
filted_img = cv2.morphologyEx(bi_img, cv2.MORPH_CLOSE, np.ones((3, 3), dtype=np.uint8))  

#骨架提取
sklted_img = ski.morphology.skeletonize(filted_img)
sklted_img_int = np.zeros([height, width], dtype=np.uint8)
for i in range(0, height):
    for j in range(0, width):
        if sklted_img[i, j]:
            sklted_img_int[i, j] = 1

#生成点列
table = [0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2,
         1, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2,
         1, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2,
         0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2,
         1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
         0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
         0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
         0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
tail_list = []
cross_list = []
kn1 = np.asarray([[1, 2, 4], [128, 0, 8], [64, 32, 16]])
marked_img = cv2.filter2D(sklted_img_int, -1, kn1, borderType=cv2.BORDER_CONSTANT)
for i in range(0, height):
    for j in range(0, width):
        cata = table[marked_img[i, j]]
        if cata == 1:
            tail_list.append([i, j])
        elif cata == 2:
            cross_list.append([i, j])
print(tail_list)
print(cross_list)